package com.kzd.sdk.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.kzd.sdk.callback.KZD_OnItemChildClickListener;
import com.kzd.sdk.callback.KZD_OnItemClickListener;
import com.kzd.sdk.entity.KZD_GameGift1;
import com.kzd.sdk.ui.adapter.holder.GameGiftHolder;
import com.kzd.sdk.utils.AppUtils;

import java.util.ArrayList;
import java.util.List;


public class KZD_GameGift1Adapter extends RecyclerView.Adapter<GameGiftHolder> {

    private List<KZD_GameGift1> data = new ArrayList<>();

    private final Context mContext;

    private KZD_OnItemClickListener mOnItemClickListener;
    private KZD_OnItemChildClickListener mOnItemChildClickListener;

    public KZD_GameGift1Adapter(Context context) {
        this.mContext = context;
    }

    @NonNull
    @Override
    public GameGiftHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(AppUtils.getLayoutId(mContext, "kzd_item_game_gift"), parent, false);
        return new GameGiftHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull GameGiftHolder holder, int position) {
        KZD_GameGift1 itemData = get(position);
        holder.tvGameGiftName.setText(itemData.getCardname());
        holder.tvGameGiftDesc.setText(itemData.getContent());
        holder.tvGameGiftNum.setText(new StringBuilder().append(itemData.getNum()).append("个"));
        holder.tvGameGiftDraw.setText(itemData.getIs_draw() == 0 ? "领取" : "已领取");
        if (itemData.getIs_draw() == 0) {
            holder.tvGameGiftDraw.setBackgroundResource(AppUtils.getDrawableId(mContext, "kzd_back_50dp_blue"));
            holder.tvGameGiftDraw.setTextColor(Color.parseColor("#ffffff"));
        } else {
            holder.tvGameGiftDraw.setBackgroundResource(AppUtils.getDrawableId(mContext, "kzd_line_50dp_gray"));
            holder.tvGameGiftDraw.setTextColor(Color.parseColor("#999999"));
        }
        holder.tvGameGiftCopy.setVisibility(View.GONE);
        holder.tvGameGiftDraw.setVisibility(View.VISIBLE);
        holder.llGameGiftCode.setVisibility(View.GONE);
        holder.llGameGiftNum.setVisibility(View.VISIBLE);
        holder.tvGameGiftDraw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemChildClickListener != null && itemData.getIs_draw() == 0) {
                    mOnItemChildClickListener.onClick(v, position);
                }
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onClick(v, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void setNewInstance(List<KZD_GameGift1> gameGiftList) {
        if (gameGiftList == null) {
            this.data = new ArrayList<>();
        } else {
            this.data = gameGiftList;
        }
        notifyDataSetChanged();
    }

    public KZD_GameGift1 get(int position) {
        return data.get(position);
    }

    public void setOnItemClickListener(KZD_OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public void setOnItemChildClickListener(KZD_OnItemChildClickListener onItemChildClickListener) {
        this.mOnItemChildClickListener = onItemChildClickListener;
    }

}
